//
//  ShopChatViewController.m
//  Findif
//
//  Created by Cicel Chen on 2018/10/23.
//  Copyright © 2018年 Cicel Chen. All rights reserved.
//

#import "ShopChatViewController.h"
#import "ChatListTableViewCell.h"
#import "ChatRoomViewController.h"
#import <JMessage/JMessage.h>

@interface ShopChatViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSMutableArray * array;

@end

@implementation ShopChatViewController

- (void)viewWillAppear:(BOOL)animated{
    
   [self getConversationList];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = ZRWhiteColor;
    
    self.array = [NSMutableArray array];
    
    [self setupUI];
    
}

- (void)setupUI{
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, topHeight, kScreenW, kScreenH - topHeight - kStatusBarHeight) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = ColorWithRGB(0xEFF0F1);
    tableView.rowHeight = 90 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.showsVerticalScrollIndicator = NO;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[ChatListTableViewCell class] forCellReuseIdentifier:@"ShopChatListTableViewCell"];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self updateData];
    }];
    
    [self.view addSubview:self.tableView];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.array.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ChatListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShopChatListTableViewCell" forIndexPath:indexPath];
    
    //    cell.title = self.titleArray[indexPath.row];
    JMSGConversation *conversation = self.array[indexPath.row];
    cell.conversation = conversation;
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HiddenBar;
    
    ChatRoomViewController * roomVC = [ChatRoomViewController new];
    JMSGConversation * conversation = self.array[indexPath.row];
    roomVC.conversation = conversation;
    [self.navigationController pushViewController:roomVC animated:YES];
    
    ShowBar;
}

- (void)getConversationList {
    
    [JMSGConversation allConversations:^(id resultObject, NSError *error) {
        
        self.array = [self sortConversation:resultObject];
        
        [self.tableView reloadData];
    }];
}


#pragma mark --排序conversation
- (NSMutableArray *)sortConversation:(NSMutableArray *)conversationArr {
    NSSortDescriptor *firstDescriptor = [[NSSortDescriptor alloc] initWithKey:@"latestMessage.timestamp" ascending:NO];
    
    NSArray *sortDescriptors = [NSArray arrayWithObjects:firstDescriptor, nil];
    
    NSArray *sortedArray = [conversationArr sortedArrayUsingDescriptors:sortDescriptors];
    
    return [NSMutableArray arrayWithArray:sortedArray];
    
    //    NSArray *sortResultArr = [conversationArr sortedArrayUsingFunction:sortType context:nil];
    //    return [NSMutableArray arrayWithArray:sortResultArr];
}


- (void)updateData{
    
    [JMSGConversation allConversations:^(id resultObject, NSError *error) {
        
        self.array = [self sortConversation:resultObject];
        
        [self.tableView reloadData];
    }];
    
    [self.tableView.mj_header endRefreshing];
}


@end
